import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import TabbarLayout from '@/views/TabbarLayout.vue'
import store from '../store'
import { loginByWXOC } from '@/api/index'
import { getToken, setToken } from '@/service/cookie'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/login',
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/',
    redirect: '/home',
    component: TabbarLayout,
    meta: { keepAlive: true },
    children: [
      {
        path: '/home',
        component: () => import('@/views/home/index.vue'),
        meta: { keepAlive: true, naem: 'Home' }
      },
      {
        path: '/demand',
        component: () => import('@/views/demand/index.vue'),
        meta: { keepAlive: true }
      },
      {
        path: '/square',
        component: () => import('@/views/square/index.vue'),
        meta: { keepAlive: true }
      },
      {
        path: '/find',
        component: () => import('@/views/find/index.vue'),
        meta: { keepAlive: true }
      },
      {
        path: '/userCenter',
        component: () => import('@/views/userCenter/index.vue'),
        meta: { keepAlive: true }
      }
    ]
  },
  {
    path: '/userQuery',
    component: () => import('@/views/userQuery/index.vue')
  },
  {
    path: '/agreement',
    component: () => import('@/views/agreement/index.vue')
  },
  {
    path: '/demand/create',
    component: () => import('@/views/demand/create.vue')
  },
  {
    path: '/filldata',
    component: () => import('@/views/filldata/index.vue')
  },
  {
    path: '/applyOf',
    component: () => import('@/views/applyOf/index.vue')
  },
  {
    path: '/applyOf/weChat',
    component: () => import('@/views/applyOf/weChat.vue')
  },
  {
    path: '/news',
    component: () => import('@/views/home/news.vue')
  },
  {
    path: '/demand/details',
    component: () => import('@/views/demand/details.vue')
  },
  {
    path: '/square/company',
    component: () => import('@/views/square/company.vue')
  },
  {
    path: '/square/contacts',
    component: () => import('@/views/square/contacts.vue')
  },
  {
    path: '/find/activity',
    component: () => import('@/views/find/activity.vue')
  },
  {
    path: '/find/actCheIn',
    component: () => import('@/views/find/actCheIn.vue')
  },
  {
    path: '/find/curriculum',
    component: () => import('@/views/find/curriculum.vue')
  },
  {
    path: '/find/free',
    component: () => import('@/views/find/free.vue')
  },
  {
    path: '/find/pay',
    component: () => import('@/views/find/pay.vue')
  },
  {
    path: '/find/bank',
    component: () => import('@/views/find/bank.vue')
  },
  {
    path: '/userCenter/needs',
    component: () => import('@/views/userCenter/myneeds.vue')
  },
  {
    path: '/userCenter/course',
    component: () => import('@/views/userCenter/mycourse.vue')
  },
  {
    path: '/userCenter/activities',
    component: () => import('@/views/userCenter/myactivities.vue')
  },
  {
    path: '/userCenter/card',
    component: () => import('@/views/userCenter/mycard.vue')
  },
  {
    path: '/userCenter/Marketing',
    component: () => import('@/views/userCenter/marketing.vue')
  },
  {
    path: '/userCenter/shaoMarketing',
    component: () => import('@/views/userCenter/shaoMarketing.vue')
  },
  {
    path: '/userCenter/member',
    component: () => import('@/views/userCenter/member.vue')
  },
  {
    path: '/userCenter/recharge',
    component: () => import('@/views/userCenter/recharge.vue')
  },
  {
    path: '/userCenter/order',
    component: () => import('@/views/userCenter/order.vue')
  },
  {
    path: '/userCenter/bank',
    component: () => import('@/views/userCenter/bank.vue')
  },
  {
    path: '/otherServices/cost',
    component: () => import('@/views/otherServices/cost.vue')
  },
  {
    path: '/otherServices/order',
    component: () => import('@/views/otherServices/order.vue')
  },
  {
    path: '/otherServices/integral',
    component: () => import('@/views/otherServices/Integral.vue')
  },
  {
    path: '/otherServices/set',
    component: () => import('@/views/otherServices/set.vue')
  },
  {
    path: '/otherServices/Invoicing',
    component: () => import('@/views/otherServices/Invoicing.vue')
  },
  {
    path: '/otherServices/information',
    component: () => import('@/views/otherServices/information.vue')
  },
  {
    path: '/enterprise/company',
    component: () => import('@/views/enterprise/company.vue')
  },
  {
    path: '/enterprise/index',
    component: () => import('@/views/enterprise/index.vue')
  },
  {
    path: '/enterprise/personnel',
    component: () => import('@/views/enterprise/personnel.vue')
  },
  {
    path: '/enterprise/qualifications',
    component: () => import('@/views/enterprise/qualifications.vue')
  },
  {
    path: '/enterprise/addPeople',
    component: () => import('@/views/enterprise/addPeople.vue')
  },
  {
    path: '/demand/complaint',
    component: () => import('@/views/demand/complaint.vue')
  },
  {
    path: '/otherServices/introduce',
    component: () => import('@/views/otherServices/introduce.vue')
  },
  {
    path: '/otherServices/contact',
    component: () => import('@/views/otherServices/contactUs.vue')
  },
  {
    path: '/userCenter/renewal',
    component: () => import('@/views/userCenter/renewal.vue')
  },
  {
    path: '/followUs',
    component: () => import('@/views/followUs/index.vue')
  },
  {
    path: '/answerSheet/form',
    component: () => import('@/views/answerSheet/form.vue')
  },
  {
    path: '/answerSheet/list',
    component: () => import('@/views/answerSheet/list.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes,
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      // 如果不是通过上述行为切换组件，就会让页面回到顶部
      return { x: 0, y: 0 }
    }
  }
})

router.afterEach(async (to, from, next) => {
  // 保存 from route
  store.dispatch('addCount', from)
  // const userId = getToken('userId')
  const token = getToken('token')
  if (!token && to.path === '/login') {
    if (window.location.href.indexOf('code') === 0 || window.location.href.indexOf('code') <= 0) {
      window.location.replace(`${import.meta.env.VITE_API_BASEURL}v1/wechat`)
      return
    } else {
      function getQueryString (name:any) {
        const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
        const r = window.location.search.substr(1).match(reg)
        if (r != null) {
          return encodeURIComponent(r[2])
        } else {
          return null
        }
      }
      const code:any = getQueryString('code')
      loginByWXOC({ code: code }).then((res: any) => {
        setToken('bindId', res.bind_id)
        setToken('token', res.token)
        if (res.token && (res.bind_id === undefined || res.bind_id === 'undefined' || !res.bind_id)) {
          store.dispatch('fetchUserInfo')
          window.location.replace('/')
        }
      })
    }
  }
})

// 全局的路由拦截
router.beforeEach((to, from, next) => {
  next()
})

export default router
